﻿#include "client.h"

Client::Client(QObject *parent) : QObject(parent)
{
    tcpSocket = new QTcpSocket(this);

    // 连接服务器成功时触发
    connect(tcpSocket, &QTcpSocket::connected, this, &Client::onConnected);

    // 当有来自服务器的数据时，触发onReadyRead槽
    connect(tcpSocket, &QTcpSocket::readyRead, this, &Client::onReadyRead);
}

void Client::startClient()
{
    tcpSocket->connectToHost("127.0.0.1", 12345);  // 连接到服务器
}

void Client::onConnected()
{
    qDebug() << "Connected to server";

    // 发送数据到服务器
    tcpSocket->write("Hello, server!");
}

void Client::onReadyRead()
{
    QByteArray data = tcpSocket->readAll();
    qDebug() << "Received from server:" << data;
}
